/**
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-25 13:02:05
 * Copyright (c) 2015- joysuch-版权所有
 */

package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.entity.BpJob;
import com.joysuch.wwyt.bp.entity.BpWorkType;
import com.joysuch.wwyt.bp.service.BpWorkTypeService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/bp/work")
@Api(tags = "/bp/work 工种")
public class BpWorkTypeController {

    private static final Logger logger = LoggerFactory.getLogger(BpWorkTypeController.class);

    @Autowired
    private BpWorkTypeService bpWorkTypeService;

    @PostMapping("add")
    @RequiresPermissions("bp:work:add")
    public Object add(@RequestBody @Valid BpWorkType entity) {
        try {
            bpWorkTypeService.add(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("创建失败", e);
            return ResultBean.fail(101, e.getMessage());
        }
    }

    @PostMapping("update")
    @RequiresPermissions("bp:work:update")
    public Object update(@RequestBody @Valid BpWorkType entity) {
        try {
            if (entity.getId() == null) {
                return ResultBean.fail(102, "未指定主键");
            }
            bpWorkTypeService.update(entity);
            return ResultBean.success("");
        } catch (Exception e) {
            logger.error("更新失败：#" + entity.getId(), e);
            return ResultBean.fail(101, e.getMessage());
        }
    }

    @RequestMapping("page")
    public ResultBean page(Pageable page) {
        try {
            page = PageDataUtils.addOrderByDesc(page, "id");
            Page<BpWorkType> data = bpWorkTypeService.findByPage(page);
            return ResultBean.pageData(data.getContent(), data.getTotalElements());
        } catch (Exception e) {
            logger.error("分页查询失败", e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }

    @PostMapping("delete")
    @RequiresPermissions("bp:work:delete")
    public Object delete(Long id) {
        bpWorkTypeService.delete(id);
        return ResultBean.success("");
    }
}
